package com.bfsuol.yihang.documentFiles.service;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bfsuol.yihang.documentFiles.api.QueryDocumentFilesService;
import com.bfsuol.yihang.documentFiles.dao.DocumentFilesMapper;
import com.bfsuol.yihang.documentFiles.domain.DocumentFilesService;
import com.bfsuol.yihang.documentFiles.dto.DocumentFilesDTO;
import com.bfsuol.yihang.documentFiles.entity.DocumentFilesEntity;
import com.bfsuol.yihang.documents.api.QueryDocumentService;
import com.bfsuol.yihang.documents.domain.DocumentService;
import com.bfsuol.yihang.documents.dto.DocumentDTO;
import com.bfsuol.yihang.documents.entity.DocumentEntity;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class QueryDocumentFilesServiceImpl implements QueryDocumentFilesService {

    @Autowired
    private DocumentFilesService documentFilesService;

    @Override
    public Page<DocumentFilesDTO> page(Page page1, DocumentFilesDTO documentParam) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if(documentParam.getDocumentId()!=null){
            queryWrapper.eq("document_id",documentParam.getDocumentId());
        }
        if(StringUtils.isNotBlank(documentParam.getVehicleNo())){
            queryWrapper.like("vehicle_no",documentParam.getVehicleNo());
        }

        Page<DocumentFilesEntity> pages = documentFilesService.page(page1,queryWrapper);
        List<DocumentFilesDTO> list = Lists.newArrayList();
        pages.getRecords().forEach(documentEntity -> {
            DocumentFilesDTO documentDTO = new DocumentFilesDTO();
            BeanUtils.copyProperties(documentEntity, documentDTO);
            list.add(documentDTO);
        });
        Page<DocumentFilesDTO> pages1 = new Page<>();
        BeanUtils.copyProperties(pages,pages1);
        pages1.setRecords(list);
        return pages1;
    }

    @Override
    public DocumentFilesDTO getById(Long id) {
        DocumentFilesDTO documentFilesDTO = new DocumentFilesDTO();
        BeanUtils.copyProperties(documentFilesService.getById(id), documentFilesDTO);
        return documentFilesDTO;
    }

    @Override
    public List<DocumentFilesDTO> findListByDocumentId(Long id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("document_id",id);
        List<DocumentFilesEntity> documentFilesEntities = documentFilesService.list(queryWrapper);
        return documentFilesEntities.stream().map(documentFilesEntity -> {
            DocumentFilesDTO documentFilesDTO = new DocumentFilesDTO();
            BeanUtils.copyProperties(documentFilesEntity, documentFilesDTO);
            return documentFilesDTO;
        }).collect(java.util.stream.Collectors.toList());
    }

}
